/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Models;

import java.sql.*;
import javax.naming.*;
import javax.sql.*;

/**
 *
 * @author VIC
 */
public class ConnectionPool {

    public Connection getConnection() {
        try {
            Connection conn = null;
            DataSource pool = null;
            Context ctx = new InitialContext();
            //Look up a DataSource, which represents a connection pool
            pool = (DataSource) ctx.lookup("java:comp/env/jdbc/iOrderpooling");
            //System.out.println("pool is: " + pool);
            if (pool != null) {
                conn = pool.getConnection();
            }
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static ResultSet Select(String sql) throws SQLException {
        ConnectionPool pool = new ConnectionPool();
        Connection conn = pool.getConnection();
        ResultSet rs = null;
        Statement stmt = conn.createStatement();
        rs = stmt.executeQuery(sql);
        return rs;
    }

    public static boolean Update(String sql) throws SQLException {
        ConnectionPool pool = new ConnectionPool();
        Connection conn = pool.getConnection();
        Statement stmt = conn.createStatement();
        int n = stmt.executeUpdate(sql);
        //stmt.close();
        //conn.close();
        if (n == 0) {
            return false;// fail
        }
        return true;
    }
}
